package com.ruoyi.web.controller.signature;

import com.ruoyi.common.core.domain.AjaxResult;
import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.http.Method;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Api(tags = "手写签名功能")
@RestController
@RequestMapping("/upload")
public class uploadPictureController {

    @Value("${minio.url}")
    private String url;

    @Value("${minio.accessKey}")
    private String accesskey;

    @Value("${minio.seccretKey}")
    private String seccretkey;

    @Value("${minio.bucketName}")
    private String bucketName;

    @ApiOperation(value = "上传手写签名图片到图片服务器")
    @PostMapping("/signature")
    public AjaxResult signature(@RequestParam(value = "file")MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("你没有上传图片");
        } else if (file.getOriginalFilename().contains("..")||!file.getOriginalFilename().contains(".")){
            return AjaxResult.error("文件格式有误");
        } else {
            String fileName = file.getOriginalFilename();
            //为防止文件名重复
            fileName = fileName.replace(".",System.currentTimeMillis()+".");
            try {
                MinioClient minioClient =
                        MinioClient.builder()
                                .endpoint(url)
                                .credentials(accesskey,seccretkey)
                                .build();

                // Make 'asiatrip' bucket if not exist.
                boolean found =
                        minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
                if (!found) {
                    // Make a new bucket called 'asiatrip'.
                    minioClient.makeBucket(MakeBucketArgs.builder().bucket("asiatrip").build());
                } else {
                    System.out.println("Bucket 'asiatrip' already exists.");
                }

                // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
                // 'asiatrip'.
                minioClient.uploadObject(
                        UploadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(fileName)
                                //TODO 此处要获取图片上传路径,现在是固定路径
                                .filename("C:\\Users\\wcy\\Pictures\\Camera Roll\\1.jpeg")
                                .contentType("image/jpeg")
                                .build());
                System.out.println("图片已上传");

               /* String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(fileName)
                        .build());*/
            } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
                System.out.println(e);
            }
        }
        return AjaxResult.success();
    }

}
